﻿/*
Copyright (c) Microsoft Corporation

All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.  You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0   

THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.  

See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. 
*/

(function () {

    WinJS.UI.processAll();

    WinJS.UI.Pages.define("/hikers.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.

        ready: function (element, options) {
            var hikersAppBar = document.getElementById('hikersAppBar');
            var listView = document.getElementById('imageOverlayLanding');
            var cmdAdd = document.getElementById('cmdAdd');
            var cmdEdit = document.getElementById('cmdEdit');
            var cmdDelete = document.getElementById('cmdDelete');

            WinJS.Utilities.query("a").listen("click", linkClickEventHandler, false);

            // respond to item invoked, navigate to selected hiker
            listView.addEventListener('iteminvoked', function (evt) {
                var state = { data: DataHikers.itemList.getAt(evt.detail.itemIndex), key: evt.detail.itemIndex };
                WinJS.Navigation.navigate('/hiker.html', state);
            });

            // show appBar when list view item selected
            listView.addEventListener('selectionchanged', function (ev) {
                this.winControl.selection.getItems().done(function (r) {
                    if (r.length < 1) {
                        hikersAppBar.winControl.hide();
                    } else {// has selection
                        showHideEditOptions();
                        hikersAppBar.winControl.show();
                    }
                });

            });

            // show edit options
            hikersAppBar.addEventListener('beforeshow', function (ev) {
                showHideEditOptions();
            });

            // edit button was selected
            cmdEdit.addEventListener('click', function (ev) {
                var indices = listView.winControl.selection.getIndices();
                if (indices.length < 1) {
                    return false;
                };

                var selectionIndex = indices[0];
                // dispatch item invoke event to go to edit screen
                listView.winControl.dispatchEvent('iteminvoked', { detail: {itemIndex: selectionIndex}});
            });

            // go to new hiker screen
            cmdAdd.addEventListener('click', function (ev) {
                listView.winControl.dispatchEvent('iteminvoked', { detail: { itemIndex: 0 } });
            });

            // delete button
            cmdDelete.addEventListener('click', function (ev) {
                var indices = listView.winControl.selection.getIndices();
                if (indices.length < 1) {
                    return false;
                };

                var selectionIndex = indices[0];
                DataHikers.itemList.splice(selectionIndex, 1);
            });

            //hide and show appbar options based on item selected, (edit and delete cant be executed on the template item)
            function showHideEditOptions() {
                var count = listView.winControl.selection.count();
                var indices = listView.winControl.selection.getIndices();
                var isTemplateItem = ((indices.length > 0) && (indices[0] == 0));
                if ((count < 1) || (isTemplateItem)) {// does not have selection, hide selection context buttons
                    cmdDelete.style.display = 'none';
                    cmdEdit.style.display = 'none';
                } else {// has selection, show selection context buttons
                    cmdDelete.style.display = '';
                    cmdEdit.style.display = '';
                }
            };

        },// end ready

        updateLayout: function (element, viewState, lastViewState) {
            /// <param name="element" domElement="true" />
            /// <param name="viewState" value="Windows.UI.ViewManagement.ApplicationViewState" />
            /// <param name="lastViewState" value="Windows.UI.ViewManagement.ApplicationViewState" />

            // TODO: Respond to changes in viewState.
        },

        unload: function () {
            // TODO: Respond to navigations away from this page.
        }
    });


})()